//
//  RestaurantParser.m
//  RegBars
//
//  Created by Huu Nhat on 7/27/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RestaurantParser.h"
#import "RegBarsAppDelegate_iPhone.h"
#import "SBJSON.h"

#define ChannelNode @"channel"
#define ItemNode	@"item"
#define LinkNode	@"link"
#define TitleNode	@"title"
#define	ThumbnailNode	@"media:thumbnail"
#define ThumbnailUrlNode @"url"

@implementation RestaurantParser
@synthesize delegate;
@synthesize resultData;

- (void)getRestaurants:(NSString *)url withDelegate:(id)aDelegate
{
	self.delegate = aDelegate;
    NSString *urlString = [NSString stringWithFormat:@"%@?city_id=%@",url,mainAppDelegate_iPhone.currentCityId];
	NSURL *baseURL = [NSURL URLWithString:[[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] retain]];	
    NSLog(@"baseURL : %@",baseURL);
	NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
	theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    resultData = [[NSMutableData alloc]init ];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.resultData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    [delegate didEndGetRestaurantForDate:date restaurantArray:nil];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    SBJSON *jsonParser = [[[SBJSON alloc] init] autorelease];
    NSString *jsonFileContent = [[[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding] autorelease];
    NSMutableArray *resultArray = (NSMutableArray*)[[jsonParser objectWithString:jsonFileContent error:nil] retain];
    if (resultArray != nil) {
        NSMutableArray *restaurantArray = [[[NSMutableArray alloc] init] autorelease];
        for (NSInteger i = 0; i < [resultArray count]; i++) {
            NSDictionary *dict = [resultArray objectAtIndex:i];
            NSLog(@"###Dict : %@",dict);
            Restaurant *restaurant = [[[Restaurant alloc] init] autorelease];
            NSString *titleString = [dict objectForKey:LinkNode];
            titleString = [titleString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            [restaurant setNameTitle:titleString];
            NSString *descString = [dict objectForKey:TitleNode];
            descString = [descString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            [restaurant setEventDescription:descString];
            [restaurant setRestaurantId:[[dict objectForKey:@"id"] intValue]];
            [restaurant setThumbnailUrl:[dict objectForKey:ThumbnailUrlNode]];
            //NSLog(@"Res - Title:%@ - id :%d des:%@", restaurant.nameTitle, restaurant.restaurantId, restaurant.eventDescription);
            [restaurantArray addObject:restaurant];            
        }
        if ([delegate respondsToSelector:@selector(didEndGetRestaurantForDate:restaurantArray:)]) {
            [delegate didEndGetRestaurantForDate:date restaurantArray:restaurantArray];
        }        
    }else {
        if ([delegate respondsToSelector:@selector(didEndGetRestaurantForDate:restaurantArray:)]) {
            [delegate didEndGetRestaurantForDate:date restaurantArray:nil];
        }        
    }    
}


- (void) dealloc{
    [resultData release];
	[super dealloc];
}

@end
